<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="标题">
          <el-input v-model="searchForm.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="searchForm.details"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addNotice">新增公告</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <!-- <el-table-column prop="noticeid" label="编号"></el-table-column> -->
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="details" label="内容" width="300" show-overflow-tooltip></el-table-column>
      <el-table-column prop="isEffect" label="图片" width="150">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.img"
            :preview-src-list="[$Url + '/upload/' + scop.row.img]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="indexno" label="排序"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="creator" label="创建人"></el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.noticeid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="isEdit?'修改公告':'新增公告'" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="addOrEditForm" ref="addOrEditNot" :rules="rules" label-width="100px" class="edit-form">
        <el-form-item label="标题：" prop="title">
          <el-input v-model="addOrEditForm.title" placeholder="请输入标题" clearable></el-input>
        </el-form-item>
        <el-form-item label="内容：" prop="details">
         <el-input type="textarea" :autosize="{ minRows: 2 }" placeholder="请输入公告内容" v-model="addOrEditForm.details"></el-input>
        </el-form-item>
        <el-form-item label="图片：" prop="img">
          <upload v-model="addOrEditForm.img"/>
        </el-form-item>
        <el-form-item label="序号：" prop="indexno">
          <el-input v-model="addOrEditForm.indexno" placeholder="请输入序号" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAddOrUpdata">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findNotice, updateNotice, delNotice, addNotice } from "@/api/user";
import upload from "@/components/uploadFile";
export default {
  components: {
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      isEdit: false,
      tableData: [],
      checkItemIds: [],
      paymentids: [],
      dialogFormVisible: false,
      dateTime: '',
      searchForm: {
        title: '',
        details: '',
      },
      addOrEditForm: {
        title: '',
        details: '',
        img: '',
        indexno: ''
      },
      rules:{
        title: [
          { required: true, message: '必须设置公告标题', trigger: 'change' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'change' }
        ],
        details: [
          { required: true, message: '必须填写公告内容', trigger: 'change' },
        ],
        indexno: [
          { required: true, message: '必须填写序号', trigger: 'change' },
          { pattern: /^\d+(\.\d+)?$/, message: '序号必须为数字值' }
        ]
      }
    }
  },
  computed: {
    queryData() {
      return {
        title: this.searchForm.title,
        details: this.searchForm.details,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findNotice.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.noticeList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSelectionChange(val) {
    },
    addNotice() {
      this.isEdit = false
      this.dialogFormVisible = true
      this.addOrEditForm = {
        title: '',
        details: '',
        img: '',
        indexno: ''
      }
    },
    edit(row) {
      this.isEdit = true
      this.dialogFormVisible = true
      const {noticeid,title,details,img,indexno} = row
      this.addOrEditForm = {
        noticeid,
        title,
        details,
        img,
        indexno
      }
    },
    saveAddOrUpdata() {
      this.$refs['addOrEditNot'].validate((valid) => {
        if (valid) {
          this.AddOrUpdata()
        } else {
          return false;
        }
      });
    },
    async AddOrUpdata() {
      if (this.isEdit) {
        const res = await updateNotice.post(this.addOrEditForm);
        if (res.resultCode === "000000") {
          this.$message.success(res.resultInfo);
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message.error(res.resultInfo);
        }
      } else {
        const res = await addNotice.post(this.addOrEditForm);
        if (res.resultCode === "000000") {
          this.$message.success(res.resultInfo);
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message.error(res.resultInfo);
        }
      }
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delNotice.post({ noticeid: id }).then(res => {
          if (res.resultCode === "000000") {
            this.$message({
              type: "success",
              message: "删除成功!"
            })
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
